package com.anolesoft.twelvePlan.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.twelvePlan.dto.MesGyInfoExport;
import com.anolesoft.twelvePlan.entity.MesGyInfoDO;
import com.anolesoft.twelvePlan.service.MesGyInfoService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyq
 * @date 2022/5/12
 * easyExcel导入
 */
@Slf4j
public class MesGyInfoListener implements ReadListener<MesGyInfoExport> {
    private static final int BATCH_COUNT = 1000;

    private List<MesGyInfoExport> list = new ArrayList<>();

    private List<MesGyInfoDO> communityList = new ArrayList<>(BATCH_COUNT);

    private MesGyInfoService mesGyInfoService;

    public MesGyInfoListener(MesGyInfoService mesGyInfoService){
        this.mesGyInfoService = mesGyInfoService;
    }

    @Override
    public void onException(Exception e, AnalysisContext analysisContext) throws Exception {

    }

    @Override
    public void invokeHead(Map<Integer, CellData> map, AnalysisContext analysisContext) {

    }

    @Override
    public void invoke(MesGyInfoExport mesGyInfoExport, AnalysisContext analysisContext) {
        //analysisContext.readWorkbookHolder().setIgnoreEmptyRow(false);
        //MesGyInfoDO mesGyInfoDO = new MesGyInfoDO();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MesGyInfoDO mesGyInfoDO =  BeanUtil.copyProperties(mesGyInfoExport, MesGyInfoDO.class);
        mesGyInfoDO.setCreateuid(loginUser.getEmpUid());
        mesGyInfoDO.setFactoryId(loginUser.getFacCode());
//;       mesGyInfoDO = MesGyInfoDO.builder()
//                .objuid(CommonUtils.stringNull(mesGyInfoExport.getObjuid()))
//                .deliveryType(CommonUtils.stringNull(mesGyInfoExport.getDeliveryType()))
//                .jx(CommonUtils.stringNull(mesGyInfoExport.getJx()))
//                .parentPartId(CommonUtils.stringNull(mesGyInfoExport.getParentPartId()))
//                .partId(CommonUtils.stringNull(mesGyInfoExport.getPartId()))
//                .djNum(mesGyInfoExport.getDjNum())
//                .innerWork(CommonUtils.stringNull(mesGyInfoExport.getInnerWork()))
//                .processRoute(CommonUtils.stringNull(mesGyInfoExport.getProcessRoute()))
//                .partType(CommonUtils.stringNull(mesGyInfoExport.getPartType()))
//                .jsWorkshop(CommonUtils.stringNull(mesGyInfoExport.getJsWorkshop()))
//                .deliveryOrder(CommonUtils.stringNull(mesGyInfoExport.getDeliveryOrder()))
//                .llWorkshop(CommonUtils.stringNull(mesGyInfoExport.getLlWorkshop()))
//                .llDeliveryOrder(CommonUtils.stringNull(mesGyInfoExport.getLlDeliveryOrder()))
//                .partName(CommonUtils.stringNull(mesGyInfoExport.getPartName()))
//                .danzhuangNum(mesGyInfoExport.getDanzhuangNum())
//                .longPeriodPart(CommonUtils.stringNull(mesGyInfoExport.getLongPeriodPart()))
//                .jmjCategory(CommonUtils.stringNull(mesGyInfoExport.getJmjCategory()))
//                .productFamily(CommonUtils.stringNull(mesGyInfoExport.getProductFamily()))
//                .processorsNew(CommonUtils.stringNull(mesGyInfoExport.getProcessorsNew()))
//                .junjian(CommonUtils.stringNull(mesGyInfoExport.getJunjian()))
//                .materialBrand(CommonUtils.stringNull(mesGyInfoExport.getMaterialBrand()))
//                .materialBrandState(CommonUtils.stringNull(mesGyInfoExport.getMaterialBrandState()))
//                .materialSpecification(CommonUtils.stringNull(mesGyInfoExport.getMaterialSpecification()))
//                .length(CommonUtils.stringNull(mesGyInfoExport.getLength()))
//                .width(CommonUtils.stringNull(mesGyInfoExport.getWidth()))
//                .height(CommonUtils.stringNull(mesGyInfoExport.getHeight()))
//                .forgedCastingsCode(CommonUtils.stringNull(mesGyInfoExport.getForgedCastingsCode()))
//                .partProperty(CommonUtils.stringNull(mesGyInfoExport.getPartProperty()))
//                .heatTreatType(CommonUtils.stringNull(mesGyInfoExport.getHeatTreatType()))
//                .bfcl(CommonUtils.stringNull(mesGyInfoExport.getBfcl()))
//                .painting(CommonUtils.stringNull(mesGyInfoExport.getPainting()))
//                .wash(CommonUtils.stringNull(mesGyInfoExport.getWash()))
//                .specialInspect(CommonUtils.stringNull(mesGyInfoExport.getSpecialInspect()))
//                .sandblasting(CommonUtils.stringNull(mesGyInfoExport.getSandblasting()))
//                .relieveStress(CommonUtils.stringNull(mesGyInfoExport.getRelieveStress()))
//                .electroPolishing(mesGyInfoExport.getElectroPolishing())
//                .others(CommonUtils.stringNull(mesGyInfoExport.getOthers()))
//                .weight(mesGyInfoExport.getWeight())
//                .remarks(CommonUtils.stringNull(mesGyInfoExport.getRemarks()))
//                .createUid(CommonUtils.stringNull(loginUser.getEmpUid()))
//                .factoryId(loginUser.getFacCode())
//                .build();
        log.debug("数据：{}", mesGyInfoDO);
        communityList.add(mesGyInfoDO);
        if (communityList.size() >= BATCH_COUNT) {
            batchInsert();
            // 存储完成清理 list
            communityList.clear();
        }
    }

    @Override
    public void extra(CellExtra cellExtra, AnalysisContext analysisContext) {

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        batchInsert();
    }

    /*
     * 重写此方法时默认返回false 需改成true
     */
    @Override
    public boolean hasNext(AnalysisContext analysisContext) {
        return true;
    }

    void batchInsert(){
        log.debug("数据：{}", communityList);
        mesGyInfoService.uploadGyInfo(communityList);
    }

}
